/*
 * 文件名：HeartBeatRespHandler.java
 * 版权：亚信联创版权所有
 * 描述：TODO
 * 修改人：xuwei3
 * 修改时间：2015-5-27
 * 修改内容：TODO
 */
package com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ailk.bigdata.etl.common.server.net.NettyUtil;
import com.ailk.bigdata.etl.realstream.server.model.MessageType;
import com.ailk.bigdata.etl.realstream.server.model.SdtpDataPack;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @description 心跳处理handler
 * @author [xuwei3]
 * @version [版本号,2015-5-27]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class HeartBeatRespHandler extends SimpleChannelInboundHandler<SdtpDataPack>
{
	private static Logger logger = LoggerFactory.getLogger(HeartBeatRespHandler.class);

	/**
	 * @see io.netty.channel.SimpleChannelInboundHandler#channelRead0(io.netty.channel.ChannelHandlerContext,
	 *      java.lang.Object)
	 */
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, SdtpDataPack dataPack) throws Exception
	{
		//如果是链路校验请求
		if (null != dataPack.getHeader() && dataPack.getHeader().getMessageType() == MessageType.linkCheck_Req.getValue())
		{
			
			logger.debug("Receive client heart beat from {} , response message to client........................", NettyUtil.getClientAdd(ctx));
			ctx.writeAndFlush(buildHeartBeatResp(dataPack));
			logger.debug("Send heart beat response message to client........................");
			
		}
		else
		{
			//发布到下面 handler
			
			ctx.fireChannelRead(dataPack);
		}
	}

	/**
	 * 
	 * @description 构造心跳返回消息
	 * @param dataPack
	 * @return
	 */
	private SdtpDataPack buildHeartBeatResp(SdtpDataPack dataPack)
	{
		dataPack.getHeader().setMessageType(MessageType.linkCheck_Resp.getValue());
		return dataPack;
	}

}
